function t(t, e) {
    wx.showLoading({
        title: "生成图片"
    }), setTimeout(function() {
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: 400 * t.deviceRatio,
            height: 400 * t.deviceRatio,
            canvasId: "tempCanvas",
            success: function(e) {
                wx.hideLoading(), t.setData({
                    tempImageSrc: e.tempFilePath
                }), wx.navigateTo({
                    url: "/pages/preview/preview?imageurl=" + (t.data.tempImageSrc ? e.tempFilePath : t.data.emotionUrl)
                }), t.setData({
                    tempImageSrc: ""
                });
            }
        });
    }, e);
}

function e(t, o, i, a) {
    return t > i ? e(i, o / (t / i), i, a) : o > a ? e(t / (o / a), a, i, a) : t <= i && o <= a ? {
        width: t,
        height: o
    } : e(t, t, i, a);
}

var o = getApp(), i = null;

Page({
    data: {
        emotionUrl: "",
        allText: [],
        isTextEdit: !1,
        isChooseFontSize: !1,
        isChooseFontColor: !1,
        isChooseFontPattern: !1,
        inputFocus: !1,
        texted: !1,
        isSelect: !1,
        bcolor: "#fff;",
        allColor: [ "#000000", "#7f7f7f", "#880015", "#ed1c24", "#ff7f27", "#fff200", "#22b14c", "#00a2e8", "#ffaec9", "#a349a4", "#ffffff", "#c3c3c3" ],
        tempImageSrc: "",
        inputText: "",
        ad: {},
        adid: "adunit-d2db016aa44dfa52",
        showad: !1,
        isFirst: !1,
        showAd: !0
    },
    onReady: function() {},
    shutAddMyMiniapp: function() {
        var t = this;
        wx.setStorageSync("isFirst", "no"), t.setData({
            isFirst: !1
        });
    },
    onLoad: function(t) {
        this.device = o.globalData.myDevice, this.deviceRatio = this.device.windowWidth / 750;
        var e = this, a = this;
        this.setData({
            emotionUrl: o.globalData.selectImageUrl,
            ad: o.globalData.ad
        }), wx.createInterstitialAd && ((i = wx.createInterstitialAd({
            adUnitId: "adunit-fc6ac07a55c81acf"
        })).onLoad(function() {}), i.onError(function(t) {}), i.onClose(function() {})), 
        wx.request({
            url: "https://ishare20.net/gifmaker/ad.json",
            headers: {
                "Content-Type": "application/json"
            },
            success: function(t) {
                o.globalData.ad = t.data, e.setData({
                    ad: o.globalData.ad
                }), i && e.data.ad.indexiad && i.show().catch(function(t) {
                    console.error(t);
                });
            }
        });
        var s = wx.getStorageSync("isFirst");
        console.log(s), s || (a.setData({
            isFirst: !0
        }), wx.setStorageSync("isFirst", "no"), setTimeout(function() {
            a.setData({
                isFirst: !1
            });
        }, 1e4));
        var n = this.data.allText;
        n = [ {
            idx: 0,
            someText: "",
            fontColor: this.fontColor ? this.fontColor : "rgba(0,0,0,1)",
            fontSize: this.fontSize ? this.fontSize : 14,
            fontStyle: "normal",
            fontWeight: "normal",
            textL: 285 * this.deviceRatio,
            textT: 435 * this.deviceRatio,
            isTextActive: !0
        } ], this.setData({
            isTextEdit: !0,
            allText: n,
            isChooseFontSize: !1,
            isChooseFontColor: !1,
            isChooseFontPattern: !1
        });
    },
    chooseaddText: function() {
        "" == this.data.emotionUrl && wx.showModal({
            title: "提示",
            content: "请先选择表情",
            showCancel: !1
        });
    },
    selectPic: function() {
        wx.navigateTo({
            url: "../emotionChoose/emotionChoose"
        }), this.setData({
            isSelect: !0
        });
    },
    clearText: function() {
        this.setData({
            inputText: "",
            allText: [ {
                idx: 0,
                someText: "",
                fontColor: this.fontColor ? this.fontColor : "rgba(0,0,0,1)",
                fontSize: this.fontSize ? this.fontSize : 14,
                fontStyle: "normal",
                fontWeight: "normal",
                textL: 285 * this.deviceRatio,
                textT: 435 * this.deviceRatio,
                isTextActive: !0
            } ],
            tempImageSrc: ""
        });
    },
    onShareAppMessage: function() {
        return {
            title: "图片表情包制作",
            path: "pages/emotionEdit/emotionEdit",
            imageUrl: "https://ishare20.net/apps/wxmini/wxb95c179e268861ca_share1.png"
        };
    },
    focusInput: function() {
        this.setData({
            inputFocus: !this.data.inputFocus
        });
    },
    inputText: function(t) {
        var e = this.data.allText;
        e[e.length - 1].someText = t.detail.value, 0 == e[e.length - 1].someText.length && (e[e.length - 1].someText = ""), 
        this.setData({
            allText: e
        });
    },
    textMoveStart: function(t) {
        this.textX = t.touches[0].clientX, this.textY = t.touches[0].clientY;
    },
    textMove: function(t) {
        var e = this.data.allText, o = t.touches[0].clientX - this.textX, i = t.touches[0].clientY - this.textY, a = 750 * this.deviceRatio - 50, s = 1e3 * this.deviceRatio - 50, n = e[e.length - 1].textL + o, h = e[e.length - 1].textT + i;
        n < 0 && (n = 0), n > a && (n = a), h < 0 && (h = 0), h > s && (h = s), e[e.length - 1].textL = n, 
        e[e.length - 1].textT = h, this.setData({
            allText: e,
            isChooseFontSize: !1,
            isChooseFontColor: !1,
            isChooseFontPattern: !1
        }), this.textX = t.touches[0].clientX, this.textY = t.touches[0].clientY;
    },
    chooseFontsize: function() {
        this.setData({
            isChooseFontSize: !this.data.isChooseFontSize,
            isChooseFontColor: !1,
            isChooseFontPattern: !1
        }), this.checkAdShow();
    },
    checkAdShow: function() {
        this.data.isChooseFontSize || this.data.isChooseFontColor || this.data.isChooseFontPattern ? this.setData({
            showAd: !1
        }) : this.setData({
            showAd: !0
        });
    },
    fontsizeSliderChange: function(t) {
        this.fontSize = t.detail.value;
        var e = this.data.allText;
        e[e.length - 1] && e[e.length - 1].isTextActive && (e[e.length - 1].fontSize = this.fontSize, 
        this.setData({
            allText: e
        }));
    },
    chooseFontColor: function() {
        this.setData({
            isChooseFontSize: !1,
            isChooseFontColor: !this.data.isChooseFontColor,
            isChooseFontPattern: !1
        }), this.checkAdShow();
    },
    fontColorChange: function(t) {
        this.fontColor = t.target.dataset.selected;
        var e = this.data.allText;
        e[e.length - 1] && e[e.length - 1].isTextActive && (e[e.length - 1].fontColor = this.fontColor, 
        this.setData({
            allText: e
        }));
    },
    chooseFontPattern: function() {
        this.setData({
            isChooseFontSize: !1,
            isChooseFontColor: !1,
            isChooseFontPattern: !this.data.isChooseFontPattern
        }), this.checkAdShow();
    },
    fontStyleChange: function(t) {
        this.fontStyle = t.detail.value ? "oblique" : "normal";
        var e = this.data.allText;
        e[e.length - 1] && e[e.length - 1].isTextActive && (e[e.length - 1].fontStyle = this.fontStyle, 
        this.setData({
            allText: e
        }));
    },
    fontWeightChange: function(t) {
        this.fontWeight = t.detail.value ? "bold" : "normal";
        var e = this.data.allText;
        e[e.length - 1] && e[e.length - 1].isTextActive && (e[e.length - 1].fontWeight = this.fontWeight, 
        this.setData({
            allText: e
        }));
    },
    cancelAddText: function() {
        this.data.allText.pop(), this.setData({
            allText: [],
            inputText: "",
            isTextEdit: !1,
            inputFocus: !1,
            isChooseFontSize: !1,
            isChooseFontColor: !1,
            isChooseFontPattern: !1
        });
    },
    competeAddText: function() {
        var o = this, i = this.data.allText;
        if ("点击输入文字" == i[i.length - 1].someText || "" == i[i.length - 1].someText) this.cancelAddText(); else {
            i[i.length - 1].isTextActive = !0, this.setData({
                allText: i,
                isTextEdit: !1,
                inputFocus: !1,
                isChooseFontSize: !1,
                isChooseFontColor: !1,
                isChooseFontPattern: !1,
                texted: !0
            });
            var a = wx.createCanvasContext("tempCanvas");
            a.fillStyle = "#fff", a.fillRect(0, 0, 400 * o.deviceRatio, 400 * o.deviceRatio), 
            wx.getImageInfo({
                src: o.data.tempImageSrc ? o.data.tempImageSrc : o.data.emotionUrl,
                success: function(s) {
                    var n = e(s.width, s.height, 400 * o.deviceRatio, 400 * o.deviceRatio), h = (400 * o.deviceRatio - n.width) / 2;
                    a.drawImage(o.data.tempImageSrc ? o.data.tempImageSrc : o.data.emotionUrl, h, 0, n.width, n.height), 
                    a.setFillStyle(i[i.length - 1].fontColor), a.font = i[i.length - 1].fontStyle + " " + i[i.length - 1].fontWeight + " " + i[i.length - 1].fontSize + "px sans-serif", 
                    a.setTextAlign("left"), a.setTextBaseline("top"), a.fillText(i[i.length - 1].someText, i[i.length - 1].textL - 170 * o.deviceRatio, i[i.length - 1].textT - 90 * o.deviceRatio), 
                    a.draw(), t(o, 200);
                }
            });
        }
    },
    saveImgToPhone: function() {
        var t = this;
        "" == this.data.emotionUrl ? wx.showModal({
            title: "提示",
            content: "请先选择表情",
            showCancel: !1
        }) : "" == this.data.allText[0].someText ? wx.navigateTo({
            url: "/pages/preview/preview?imageurl=" + this.data.emotionUrl
        }) : (wx.showLoading({
            title: "检测文本"
        }), wx.request({
            url: o.globalData.apiUrl + "/checkText",
            data: {
                text: this.data.allText[0].someText
            },
            success: function(e) {
                wx.hideLoading(), e.data.success ? (wx.showToast({
                    title: "检查通过"
                }), t.competeAddText()) : wx.showToast({
                    title: e.data.msg,
                    icon: "none"
                });
            },
            fail: function(t) {
                wx.hideLoading(), wx.showToast({
                    title: "连接出错",
                    icon: "none"
                });
            }
        }));
    },
    onShow: function() {
        this.setData({
            emotionUrl: o.globalData.selectImageUrl
        });
    }
});